package com.eds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eds.exception.BusException;
import com.eds.feign.CourseFeign;
import com.eds.mapper.TeacherMapper;
import com.eds.pojo.CodeEnum;
import com.eds.service.TeacherService;
import com.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：TeacherServiceImpl
 * @Author: vvSmile
 * @Date: 2025/03/27
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private CourseFeign courseFeign;


    /* *
     * @Title: selectTeacherInfoByUserId
     * @Author: vvSmile
     * @Date: 2025-03-29 09:55:32
     * @param userId
     * @return Teacher
     * @Description: TODO 根据用户Id查询老师信息
     */
    @Override
    public Teacher selectTeacherInfoByUserId(Long userId) {
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("user_id", userId);
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);
        if (teacher == null) {
            throw new BusException(CodeEnum.SELECT_USER_INFO_BY_USERID_ERROR.getCode(), CodeEnum.SELECT_USER_INFO_BY_USERID_ERROR.getMessage());
        }
        return teacher;
    }


    /* *
     * @Title: registerService
     * @Author: vvSmile
     * @Date: 2025-03-29 09:55:54
     * @param teacher
     * @return boolean
     * @Description: TODO 注册
     */
    @Override
    public boolean registerService(Teacher teacher) {
        int insert = teacherMapper.insert(teacher);
        return insert > 0;
    }

    @Override
    public int deleteTeacher(Long userId) {
        int i1 = teacherMapper.deleteById(userId);
        int i = courseFeign.deleteCourseByTeacherId(userId);
        if (i > 0 && i1 > 0) {
            return 1;
        }
        return 0;
    }
}
